<template>
	<view class="container">
		<view class="bg-white" >
			<!-- <kg-custom :isBack="true" bgColor="bg-white">
				<block slot="left">
					<view class="flex justify-start align-center bg-F4F4F4 padding-left round margin-left-sm"
						style="height: 70rpx;width: 460rpx;">
						<image :src="baseUrl + '/static/search_for.png'" mode="" style="width: 25rpx;height: 25rpx;">
						</image>
						<input type="text" @input='handInput' v-model="shopName" value="" class="text-13 f-grow1 padding-left-sm" placeholder="店铺名字" />
					</view>
				</block>
			</kg-custom> -->
			<view class="text-666 text-df flex justify-between align-center nav">
				<scroll-view scroll-x class="bg-white nav text-666 position" style="width:100%;" scroll-with-animation
					:scroll-left="scrollLeft">
					<view class="cu-item" :class="TabCur==999?'color cur text-bold':''" @tap="tabSelect" :data-id="999">
						<view>全部</view>
					</view>
					<view class="cu-item" :class="item.id==TabCur?'color cur text-bold':''"
						v-for="(item,index) in classification1" @tap="tabSelect" :data-id="item.id">
						<view>{{item.name}}</view>
					</view>
				</scroll-view>
			</view>
		</view>
		<view style="margin-bottom: 160rpx;margin-top: 120rpx;">
			<view class="bg-white radius10 padding-sm margin-sm" v-for='(item,index) in list' :key='index'>
				<view class="flex justify-between align-center text-bold">
					<view class="text-15 text-333">
						订单编号 {{item.orderNo}}
					</view>
					<view class="text-sm color">
						<block v-if='item.status==0'>待付款</block>
						<block v-if='item.status==1'>待发货</block>
						<block v-if='item.status==2'>配送中</block>
						<block v-if='item.status==3'>已收货</block>
						<block v-if='item.status==4'>已评价</block>
						<block v-if='item.status==5'>已取消</block>
					</view>
				</view>
				<view class="flex justify-start margin-top" v-for='(value,i) in item.itemList' :key='i'>
					<image @click="toPage('/filePage/pages/goods-detail/goods-detail?id=' + value.productId)"    :src="value.productPic" mode="" style="width: 166rpx;height: 166rpx;min-width: 166rpx;"></image>
					<view class="margin-left-sm flex-treble text-13 text-999">
						<view class="text-cut-2 text-333">
							采购来源 {{item.shopName}}
						</view>
					<!-- 	<view class="margin-top-sm">
							采购种类:2
						</view> -->
						<view class="">
							采购数量:{{value.num}}
						</view>
					</view>
				</view>
				<view class="flex justify-end align-center text-df margin-top">
					<view class="text-999">
						应付金额 ¥ {{parseFloat(item.productAmount/100).toFixed(2)}}
					</view>
					<view class="text-333 margin-left-xs">
						实付金额 ¥ {{parseFloat(item.realAmount/100).toFixed(2)}}
					</view>
				</view>
				<view  class="flex justify-end align-center margin-top">
					<!-- <button class="btn2 round text-13 text-white text-999">取消订单</button> -->
					<button v-if='item.status==0' @click='wxPay(item)'  class="btn2 round text-13 text-white color margin-left">支付</button>
					<button v-if='item.status==0' @click='cancel(item)'  class="btn2 round text-13 text-white color margin-left">取消</button>
					<button v-if='item.status==2' @click='checkOutState(item)'  class="btn2 round text-13 text-white color margin-left">收货</button>
					<button v-if='item.status==3' @click="toPage2('/filePage/pages/productAddComment/productAddComment?item=',item)"  class="btn2 round text-13 text-white color margin-left">评论</button>
					<button v-if='item.status==4' @click.stop="toPage('/filePage/pages/orderRecommend/orderRecommend?id='+item.id+'&type=2')"  class="btn2 round text-13 text-white color margin-left">评论列表</button>
				</view>
			</view>
		</view>
		
		<button @click="toPage('/filePage/pages/purchase/purchase')"
			class="padding bg-linear-FF416C text-white round footer flex justify-center align-center">
			<image :src="baseUrl + '/static/cart3.png'" mode="" class="margin-right-xs"></image>
			去采购
		</button>
		<w-picker :visible.sync="visible" mode="selector" :default-props="defaultProps"  default-type="id" value="1" :options="selectorList" @confirm="onConfirm($event)"
			@cancel="onCancel" ref="selector">
		</w-picker>
		<passkeyborad :money='money' @message='message' @close='close' :show="show"></passkeyborad>
		<uni-load-more :status="loading" />
	</view>
</template>

<script>
	import BetterPull from "@/common/util/BetterPull";
	import passkeyborad from '@/components/yzc-paykeyboard/yzc-paykeyboard.vue'
	export default {
		mixins: [
			BetterPull({
				async getPage(page, done) {
					this.loading = "loading";
					const param = {
						type:2,
						// status: this.status,
						pageNum: page,
						pageSize: 10,
					}
				if (this.TabCur != 999) {
					console.log(this.TabCur)
					param['status'] = this.TabCur
				}
				if(this.shopName){
					param['shopName'] = this.shopName
				}

					const [err, res] = await this.$get(this.$api.myList, param);
					if (res) {
						var data = res.data.list;
						done(data);
						if (data.length < 10) {
							this.loading = "noMore";
						} else {
							this.loading = "more";
						}
					}
					if (err) {
						done([]);
						this.loading = "noMore";
					}
				},
			}),
		],
		components: {
			passkeyborad
		},
		data() {
			return {
				baseUrl: this.$config.baseUrl,
				loading: "more",
				TabCur: '999',
				scrollLeft: 0,
				shopName:'',
				classification1: [{
						name: '待付款',
						id: '0'
					}, {
						name: '待发货',
						id: '1'
					}, {
						name: '配送中',
						id: '2'
					}, {
						name: '已收货',
						id: '3'
					},
					{
						name: '已评价',
						id: '4'
					},
					{
						name: '已取消',
						id: '5'
					}

				],
				selectorList: [{
					name: '微信',
					id: 1
				}, {
					name: '余额支付',
					id: 2
				}],
				defaultProps:{"label":"name","value":"id"},
				visible: false,
				id: '',
				show: false,
				password: 0,
				money: 0,
				index:1
			};
		},
		onUnload() {
			uni.switchTab({
				url:'/pages/file/file'
			})
		},
		methods: {
			close() {
				console.log(111111)
				this.show = false
			},
			message(password) {
				console.log(password)
				var CusBase64 = require('@/common/js/base64.js');
				//函数调用
				var passWord = CusBase64.CusBASE64.encoder(password);
			this.$get(this.$api.verifyPayPassWord, {
					passWord,
					userId: uni.getStorageSync('userId')
				}).then(res=>{
					 const [err,rs] = res
					 if(rs){
						const data = {
							orderId: this.id
						};
						 this.$post(this.$api.order_accountPay, data).then(result=>{
							 const [err,rs]  = result
							 if(rs){
								 this.$util.msg(rs.message)
								 this.show = false
								 this.Refresh()
								
							 }
						 })
						 
					 }
					 if(err){
					 	this.show = false
					 	if(err.message=='请去设置密码！'){
					 		setTimeout(()=>{
					 		uni.navigateTo({
					 			url: '/pagesA/pages/setPassword/setPassword'
					 		})
					 	},1500)
					 	}
					 }
				})
			
			},
			wxPay(item) {
				this.visible = true
				this.id = item.id
				this.money = item.realAmount
			},
			toPage2(url, item) {
				const obj = {
					id: item.id,
					itemList: item.itemList,
				}
				this.$util.openNewPage(url + JSON.stringify(obj))
			},
			toPage(url) {
				this.$util.openNewPage(url)
			},
			onCancel() {
				this.visible = false
			},
			async onConfirm(item) {
				console.log(item)
				const that = this
				const data = {
					orderId: this.id
				};
				if (item.value == 1) {
					const [error, rs] = await this.$post(this.$api.order_wxPay, data);
					if (rs) {
						console.log(rs);
						// uni.hideLoading();
						const data = rs.data;
						// #ifdef  MP-WEIXIN
						uni.requestPayment({
							provider: "wxpay",
							appId: data.appId,
							timeStamp: data.timeStamp,
							nonceStr: data.nonceStr,
							package: data._package,
							signType: data.signType,
							paySign: data.paySign,
							success: function(res) {
								console.log("success:" + JSON.stringify(res));
								that.Refresh()
									
							},
							fail: function(err) {
								console.log("fail:" + JSON.stringify(err));
							},
						});
						// #endif
						// #ifdef  APP-PLUS
						uni.requestPayment({
							"provider": "wxpay",
							"orderInfo": {
								"appid": "wx499********7c70e", // 微信开放平台 - 应用 - AppId，注意和微信小程序、公众号 AppId 可能不一致
								"noncestr": "c5sEwbaNPiXAF3iv", // 随机字符串
								"package": "Sign=WXPay", // 固定值
								"partnerid": "148*****52", // 微信支付商户号
								"prepayid": "wx202254********************fbe90000", // 统一下单订单号 
								"timestamp": 1597935292, // 时间戳（单位：秒）
								"sign": "A842B45937F6EFF60DEC7A2EAA52D5A0" // 签名，这里用的 MD5 签名
							},
							success(res) {
								that.Refresh()
							},
							fail(e) {}
						})
						// #endif
						
						
					}
				} else {
					this.show = true
					// const [error, res] = await this.$post(this.$api.order_accountPay, data);
					// if (res) {
					// 	// console.log(res);
					// 	this.$util.msg(res.message)
					// 	 that.Refresh()
					// 	uni.hideLoading();
			
					// }
				}
			
			},
			toPage(url) {
				this.$util.openNewPage(url)
			},
			tabSelect(e) {
				console.log(e)
				if (e.currentTarget.dataset.id != 999) {
					this.scrollLeft = (e.currentTarget.dataset.id - 1) * 60
				}
				this.TabCur = e.currentTarget.dataset.id;
				this.Refresh();
			},
			cancel(item){
				const that = this
				uni.showModal({
					title: '提示',
					content: '你确定取消该订单吗',
					success: function(res) {
						if (res.confirm) {
							console.log('用户点击确定');
							const data = {
								orderId: item.id,
								orderStatus: 5
							}
							that.$get(that.$api.confrimProduct, data).then(res => {
								const [err, rs] = res
								if (rs) {
									that.$util.msg(rs.message)
									that.Refresh();
								}
							})
				
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			
			checkOutState(item) {
				const that = this
				uni.showModal({
					title: '提示',
					content: '你确定收货吗',
					success: function(res) {
						if (res.confirm) {
							console.log('用户点击确定');
							const data = {
								orderId: item.id,
								orderStatus:3
							}
							that.$get(that.$api.confrimProduct, data).then(res => {
								const [err, rs] = res
								if (rs) {
									that.$util.msg(rs.message)
									that.Refresh();
								}
							})
			
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.nav {
		.on {
			border-bottom: 6rpx solid #ED7200;
			font-weight: bold;
			color: #221815;
		}

		view {
			border-bottom: 6rpx solid #fff;
		}
	}

	.btn2 {
		padding: 20rpx 24rpx;
		border: 2rpx solid;
	}
	.position{
		position: fixed;
		background-color: #fff;
		z-index: 99;
		top:0rpx;
	}

	.footer {
		position: fixed;
		left: 20rpx;
		right: 20rpx;
		bottom: 98rpx;

		image {
			width: 48rpx;
			height: 48rpx;
		}
	}
</style>
